package com.bootdo.wx.service.impl;

import cn.hutool.core.map.MapUtil;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.dao.GoodsGoodsDao;
import com.bootdo.wx.dao.GoodsProductDao;
import com.bootdo.wx.dao.MemberCartDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.GoodsProductDO;
import com.bootdo.wx.domain.MemberCartDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.GoodsGoodsService;
import com.bootdo.wx.service.MemberCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class MemberCartServiceImpl implements MemberCartService {
	@Autowired
	private MemberCartDao memberCartDao;
	@Autowired
	private GoodsProductDao goodsProductDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private GoodsGoodsDao goodsGoodsDao;
	@Autowired
	private GoodsGoodsService goodsGoodsService;


	@Override
	public MemberCartDO get(String id){
		return memberCartDao.get(id);
	}

	@Override
	public List<MemberCartDO> list(Map<String, Object> map){
		return memberCartDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return memberCartDao.count(map);
	}

	@Override
	public int save(MemberCartDO memberCart){
		return memberCartDao.save(memberCart);
	}

	@Override
	public int update(MemberCartDO memberCart){
		return memberCartDao.update(memberCart);
	}

	@Override
	public int remove(String id){
		return memberCartDao.remove(id);
	}

	@Override
	public int batchRemove(String[] ids){
		return memberCartDao.batchRemove(ids);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R add(Map<String, Object> map) {
		String userId = map.get("userId").toString();
		String goodsId = map.get("goodsId").toString();
		String productId = map.get("productId").toString();
		String address_id = map.get("addressId").toString();
		int num = Integer.parseInt(map.get("num").toString());

		//查询这个用户的购物车
		MemberCartDO memberCartDO = memberCartDao.queryByMemberGoodsAndProduct(map);
		//判断是否为空
		if (memberCartDO != null){
			int cartNum = memberCartDO.getNum();
			num = num+cartNum;
			memberCartDO.setNum(num);
//			BigDecimal allMoney =  productPrice.multiply(new BigDecimal(num));
//			memberCartDO.setPrice(allMoney);
			memberCartDao.update(memberCartDO);
		}else {
			memberCartDO = new MemberCartDO();
			memberCartDO.setId(UuidUtil.get32UUID());
			memberCartDO.setUserId(userId);
			memberCartDO.setGoodsId(goodsId);
			memberCartDO.setProductId(productId);
			memberCartDO.setAddressId(address_id);
			memberCartDO.setOpAt(new Date());
			//sku
			GoodsProductDO goodsProductDO = goodsProductDao.get(productId);
			if (goodsProductDO==null){
				return R.error("产品异常");
			}

			memberCartDO.setSku(goodsProductDO.getSku());
			memberCartDO.setNum(num);
			memberCartDO.setDelFlag(0);
			memberCartDao.save(memberCartDO);
		}
		return R.ok();
	}

	@Override
	public List<Map<String, Object>> getCartListBuyUser(Map<String, Object> map) {
		String userId = com.bootdo.common.utils.StringUtils.getString(map.get("userId"));
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		int level = 0;
		if (memberUserDO != null) {
			map.put("level", memberUserDO.getLevel());
			level = memberUserDO.getLevel();
		} else {
			map.put("level", 0);

		}
		List<Map<String, Object>> cartList = memberCartDao.getCartListBuyUser(map);
		for (Map<String, Object> goodsMap : cartList) {
			//int isvip = Integer.valueOf(goodsMap.get("isvip").toString());
			String imgurl = goodsMap.get("imgurl").toString();
			List<String> imgList = new ArrayList<String>();
			imgList.add(imgurl);
			goodsMap.remove("imgurl");
			goodsMap.put("imgList",imgList);
			Object price = goodsMap.get("price");
			Object price_c = goodsMap.get("price_c");
			Object price_z = goodsMap.get("price_z");
			Object price_g = goodsMap.get("price_g");
			switch (level) {
				case 1 :
					goodsMap.put("minePrice",price_c);
					break;
				case 2 :
					goodsMap.put("minePrice",price_z);
					break;
				case 3 :
					goodsMap.put("minePrice",price_g);
					break;
				default:
					goodsMap.put("minePrice",price);
					break;
			}
			if(null!=goodsMap.get("spikeEnd")&&!"".equals(goodsMap.get("spikeEnd"))){
				Date spikeEnd = MapUtil.getDate(goodsMap, "spikeEnd");
				if(spikeEnd.before(new Date())){
					goodsMap.put("invalid",1);
				}
			}
			goodsMap.put("invalid",0);

		}


		return cartList;
	}

	@Override
	public int removeByUserIdAndCardId(Map<String, Object> map) {
		return memberCartDao.removeByUserIdAndCardId(map);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R editCart(Map<String, Object> map) {
		String cartId = map.get("cartId").toString();
		String userId = map.get("userId").toString();
		Integer num = Integer.valueOf(map.get("num").toString());
		MemberCartDO memberCartDO = memberCartDao.get(cartId);
		if (memberCartDO==null){
			return R.error("商品信息异常");
		}
		if (!userId.equals(memberCartDO.getUserId())){
			return R.error("用户信息异常");
		}
		Map<String,Object> canMap = new HashMap<String,Object>();
		canMap.put("xId",memberCartDO.getGoodsId());
		canMap.put("type",0);
		canMap.put("productId",memberCartDO.getProductId());
		canMap.put("num",num);
		canMap.put("userId",userId);
		R r = goodsGoodsService.isCanBye(canMap);
		if (Integer.valueOf(r.get("code").toString()) != 0) {
			return r;
		}

		MemberCartDO cartDO = new MemberCartDO();
		cartDO.setId(memberCartDO.getId());
		cartDO.setNum(num);
		memberCartDao.update(cartDO);
		return R.ok();
	}

	@Override
	public List<Map<String, Object>> getCartListBuyIds(Map<String, Object> map) {
		return memberCartDao.getCartListBuyIds(map);
	}


}
